package com.cheer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.dao.OptLogDao;
import com.cheer.entity.OaAdminEntity;
import com.cheer.entity.OaLearnClassEntity;
import com.cheer.entity.OptLogEntity;
import com.cheer.param.OptLogShowParam;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.OaAdminService;
import com.cheer.service.OptLogService;
import com.cheer.vo.OptLogVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;


@Service("optLogService")
public class OptLogServiceImpl extends ServiceImpl<OptLogDao, OptLogEntity> implements OptLogService {

    private static final Logger LOGGER = LoggerFactory.getLogger(OptLogServiceImpl.class);

    @Autowired
    private OptLogDao optLogDao;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Autowired
    private OaAdminService oaAdminService;

    @Override
    public void insertLog(String action, String tableName, String keyWord, Long optId){

        try {
            Integer adminId = userInfoHolder.getAdmin().getId();
            OptLogEntity optLogEntity = new OptLogEntity();
            optLogEntity.setAction(action);
            optLogEntity.setOptId(optId);
            optLogEntity.setTableName(tableName);
            optLogEntity.setKeyWord(keyWord);
            optLogEntity.setOperator(adminId);
            save(optLogEntity);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("记录日志失败：{}，{}，{}", action, tableName, keyWord);
        }
    }

    @Override
    public IPage<OptLogVO> showLog(OptLogShowParam optLogShow){

        QueryWrapper<OptLogEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<OptLogEntity> lambda = queryWrapper.lambda();
        lambda.eq(OptLogEntity::getTableName, optLogShow.getTableName());
        if(StrUtil.isNotBlank(optLogShow.getKeyWord())){
            lambda.like(OptLogEntity::getKeyWord, optLogShow.getKeyWord());
        }
        if(ObjectUtil.isNotNull(optLogShow.getStartTime())){
            lambda.ge(OptLogEntity::getCreateTime, optLogShow.getStartTime());
        }
        if(ObjectUtil.isNotNull(optLogShow.getEndTime())){
            lambda.le(OptLogEntity::getCreateTime, optLogShow.getEndTime());
        }
        IPage<OptLogEntity> page = new Page<>(optLogShow.getPageNumber(), optLogShow.getPageSize());
        optLogDao.selectPage(page, queryWrapper);

        Function<OptLogEntity , OptLogVO> convert = entity -> {
            OptLogVO optLogVO = new OptLogVO();
            BeanUtil.copyProperties(entity, optLogVO);
            OaAdminEntity oaAdmin = oaAdminService.getById(entity.getOperator());
            optLogVO.setAdTel(oaAdmin.getAdTel());
            optLogVO.setAdName(oaAdmin.getAdName());
            return optLogVO;
        };
        return page.convert(convert);

    }

    @Override
    public void saveBatchLog(String action, String tableName , List<OaLearnClassEntity>  oaLearnClassAddDTOS) {

        List<OptLogEntity> list = new ArrayList<>();
        for (int i = 0; i < oaLearnClassAddDTOS.size(); i++) {
            OaLearnClassEntity oaLearnClassAddDTO = oaLearnClassAddDTOS.get(i);
            try {
                Integer adminId = userInfoHolder.getAdmin().getId();
                OptLogEntity optLogEntity = new OptLogEntity();
                optLogEntity.setAction(action);
                optLogEntity.setTableName(tableName);
                optLogEntity.setKeyWord(oaLearnClassAddDTO.getName());
                optLogEntity.setOperator(adminId);
                optLogEntity.setOptId(Long.valueOf(oaLearnClassAddDTO.getId()));
                list.add(optLogEntity);
            } catch (Exception e) {
                e.printStackTrace();
                LOGGER.error("记录日志失败：{}，{}，{}", action, tableName, oaLearnClassAddDTO.getName());
            }
        }
        saveBatch(list);
    }


}